package cn.kxx.config;

import cn.kxx.job.ClearImgJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;

/**
 * 定时任务配置类
 */
@Configuration
public class QuartzConfig {

    @Bean
    public ClearImgJob clearImgJob(){

        return new ClearImgJob();
    }

    /**注册JobDetailFactoryBean*/
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        //配置定时任务
        jobDetailFactoryBean.setJobClass(ClearImgJob.class);
        //定时任务持久化
        jobDetailFactoryBean.setDurability(true);
        return jobDetailFactoryBean;
    }

    /**配置触发器*/
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(){
        CronTriggerFactoryBean cronTrigger = new CronTriggerFactoryBean();
        cronTrigger.setJobDetail(jobDetailFactoryBean().getObject());
        //配置时间规则
        cronTrigger.setCronExpression("0 0/10 * * * ? *");
//        cronTrigger.setCronExpression("20/20 0 0/2 * * ? ");
        return cronTrigger;
    }
}